﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Bootstrap.MVC.Common;
using Bootstrap.MVC.Infrastructure.Html;
using Bootstrap.MVC.Utils;

namespace Bootstrap.MVC.Widgets
{
    public class DropDownItemHtmlGenerator : HtmlGeneratorBindableBase<IDropDownItemModel>
    {
        public DropDownItemHtmlGenerator(IDropDownItemModel model) : base(model) { }



        //<div class="btn-group">
        //    <button class="btn dropdown-toggle" data-toggle="dropdown">Action <span class="caret"></span></button>
        //    <ul class="dropdown-menu">
        //        <li><a href="#">Action</a></li>
        //        <li><a href="#">Another action</a></li>
        //        <li><a href="#">Something else here</a></li>
        //        <li class="divider"></li>
        //        <li><a href="#">Separated link</a></li>
        //        <li class="dropdown-submenu">
        //             <a tabindex="-1" href="#">More options</a>
        //             <ul class="dropdown-menu">
        //               <li><a tabindex="-1" href="#">Second level link</a></li>
        //               <li><a tabindex="-1" href="#">Second level link</a></li>
        //               <li><a tabindex="-1" href="#">Second level link</a></li>
        //               <li><a tabindex="-1" href="#">Second level link</a></li>
        //               <li><a tabindex="-1" href="#">Second level link</a></li>
        //             </ul>
        //        </li>
        ////    </ul>
        //</div>

        public override HtmlTagBuilder AsHtmlBuilder()
        {
            var liTag = new HtmlTagBuilder("li", Model.Attributes);

            if (Model.SubMenu.Items.Count == 0)
            {
                if (Model.Disabled)
                    liTag.AddClass("disabled");

                if (Model.Divider)
                {
                    liTag.AddClass("divider");
                }
                else
                {
                    var aTag = new HtmlTagBuilder("a").Attr("href", Model.Url).Text(Model.Text);

                    if (!Model.Disabled)
                    {
                        foreach (var e in Model.Events.EventBag)
                        {
                            aTag.Attr(e.EventName, "{0}()".FormatWith(e.Handler.Replace("()", "")));
                        }
                    }
                    liTag.Append(aTag);
                }
            }
            else
            {
                liTag.AddClass("dropdown-submenu");

                if (Model.Disabled)
                    liTag.AddClass("disabled");
                
                if (Model.Position == LeftOrRight.Left)
                    liTag.AddClass("pull-left");

                liTag.Append(
                               new HtmlTagBuilder("a")
                                   .Attr(new { herf = "#", tabindex = -1 })
                                   .Text(Model.Text)
                             );

                if (!Model.Disabled)
                    liTag.Append(new DropDownMenuHtmlGenerator(Model.SubMenu).AsHtmlBuilder());
                
            }
            return liTag;
        }

        public override string Render()
        {
            return AsHtmlBuilder().Render();
        }
    }
}
